package com.kuanter.kuanterauto.share;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.utils.Util;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;

public class WeixinShareUtil {
	private Activity activity;
	public static String AppID = MainConstants.WEIXIN_APP_ID;
	private static IWXAPI api = null;

	public WeixinShareUtil(Activity activity) {
		this.activity = activity;
		api = getInstace();
	}

	public IWXAPI getInstace() {
		if (api == null) {
			api = WXAPIFactory.createWXAPI(activity, AppID, false);
			return api;
		} else
			return api;
	}

	// 分享文本
	public void shareText(String text) {
		api.registerApp(AppID);
		// 发送了
		WXTextObject textObj = new WXTextObject();
		textObj.text = text;

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = textObj;
		// msg.title = "Will be ignored";
		msg.description = text;

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("text"); // transaction
		req.message = msg;

		api.sendReq(req);
		//activity.finish();
	}
	
	public void shareImage(int rId){
		Bitmap bmp = BitmapFactory.decodeResource(activity.getResources(), rId);
		WXImageObject imgObj = new WXImageObject(bmp);
		
		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;
		
		Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
		bmp.recycle();
		msg.thumbData = Util.bmpToByteArray(thumbBmp, true); 

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("img");
		req.message = msg;
		api.sendReq(req);
		
		activity.finish();
	}

	// 分享图片
	public void shareImageURL(String url ) {

		WXImageObject imgObj = new WXImageObject();
		imgObj.imageUrl = url;

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;

		Bitmap bmp = null;
		try {
			bmp = BitmapFactory.decodeStream(new URL(url).openStream());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150,
				150, true);
		bmp.recycle();
		msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("img");
		req.message = msg;
		api.sendReq(req);

		//activity.finish();

	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}
}
